package fu.capstone.hanoicityguide.presentation.fragment.tour;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.UiLifecycleHelper;

import fu.capstone.hanoicityguide.R;
import fu.capstone.hanoicityguide.business.bo.tour.TourBO;
import fu.capstone.hanoicityguide.business.bo.tour.TourBOImp;
import fu.capstone.hanoicityguide.business.model.Tour;
import fu.capstone.hanoicityguide.presentation.adapter.ViewPagerAdapter;

;

@SuppressLint("ValidFragment")
public class DetailTourFragment extends Fragment {

	private TourBO tourBO;
	private int tourId;
	private ViewPager imgFragment;
	private ViewPagerAdapter vpagerAdapter;
	private TextView tourDescription;
	private TextView tourInformation;
	private UiLifecycleHelper uiHelper;

	public DetailTourFragment() {

	}

	public DetailTourFragment(int tourId) {
		this.tourId = tourId;
	}

	public int getTourId() {
		return this.tourId;
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if (uiHelper != null) {
			uiHelper.onDestroy();
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		setHasOptionsMenu(true);
		// list attraction by filter and sorter
		View browseTourFragment = inflater.inflate(
				R.layout.fragment_detail_tour, container, false);

		tourBO = new TourBOImp(container.getContext());

		// get data from database
		Tour tour = tourBO.browseTour(getTourId());

		// fill data into components
		tourDescription = (TextView) browseTourFragment
				.findViewById(R.id.txtTourDescription);
		tourInformation = (TextView) browseTourFragment
				.findViewById(R.id.txtTourInformation);

		tourDescription.setText(tour.getDescription());

		String information = "";
		if (!"".equals(tour.getProviderName().trim())) {
			information = information.concat("Tourism provider : ");
			information = information.concat(tour.getProviderName().trim());
			information = information.concat("\n");
		}
		if (!"".equals(tour.getPhoneNumber().trim())) {
			information = information.concat("Phone number : ");
			information = information.concat(tour.getPhoneNumber().trim());
			information = information.concat("\n");
		}
		if (!"".equals(tour.getDuration() + "".trim())) {
			information = information.concat("Duration : ");
			if (tour.getDuration() > 1) {
				information = information.concat(tour.getDuration() + " days");
			} else {
				information = information.concat(tour.getDuration() + " day");
			}

			information = information.concat("\n");
		}
		if (!"".equals(String.valueOf(tour.getPrice()).trim())) {
			information = information.concat("Price : About ");
			information = information.concat(tour.getPrice() + " VNĐ");
			information = information.concat("\n");
		}

		tourInformation.setText(information);

		// FragmentActivity fma = (FragmentActivity) getActivity();

		imgFragment = (ViewPager) browseTourFragment
				.findViewById(R.id.tourPager);

		vpagerAdapter = new ViewPagerAdapter(getActivity(), tour.getArrImage());

		imgFragment.setAdapter(vpagerAdapter);

		return browseTourFragment;
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		// TODO Auto-generated method stub
		super.onCreateOptionsMenu(menu, inflater);
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		getActivity().getActionBar().setTitle(
				tourBO.browseTour(tourId).getName());
	}
}
